# -*- coding: utf-8 -*-

import sys
import threading
import traceback
from Guild import *

class Command(threading.Thread) :
	def __init__(self, client) :
		threading.Thread.__init__(self)
		self.client = client

		self.func = dict()
		self.func[JoinGuildAck.MSG_ID] = self.Ack_JoinGuild
		self.func[LeaveGuildAck.MSG_ID] = self.Ack_LeaveGuild
		self.func[KickoutGuildMemberAck.MSG_ID] = self.Ack_KickoutGuild
		self.func[DisbandGuildAck.MSG_ID] = self.Ack_DisbandGuild

		self.func[CreateGuildAck.MSG_ID] = self.Ack_CreateGuildAck
		self.func[GuildInfoAck.MSG_ID] = self.Ack_GuildInfoAck
		self.func[GuildMemberAck.MSG_ID] = self.Ack_GuildMemberAck
		self.func[CreateGuildAck.MSG_ID] = self.Ack_CreateGuildAck

	def getFunc(self, msg) :
		return self.func.get(msg)


	def Syn_GuildInfoSyn(self, seq) :
		print traceback.extract_stack()[-1][2]
		req = GuildInfoSyn()
		req.user_seq = seq
		req.guild_seq = 1
		self.client.send(req)
		
	def Ack_GuildInfoAck(self, recv_data) :
		print traceback.extract_stack()[-1][2]
		ack = GuildInfoAck()
		ack.Load(recv_data)
		print 'result(%d) guild(%s, %d, %s)' % (ack.result, ack.stInfo.name, ack.stInfo.master_seq, ack.stInfo.master_nickname)
		
		
	def Syn_GuildMemberSyn(self, seq) :
		print traceback.extract_stack()[-1][2]
		req = GuildMemberSyn()
		req.user_seq = seq;
		req.guild_seq = 1
		self.client.send(req)
		
	def Ack_GuildMemberAck(self, recv_data) :
		print traceback.extract_stack()[-1][2]
		ack = GuildMemberAck()
		ack.Load(recv_data)
		print 'result(%d)' % (ack.result)
		for member in ack.array_members :
			print 'member: %d, %s, %s, %d' % (member.seq, member.nickname, member.socialid, member.level)


	def Syn_LoginGuildMemberSyn(self) :
		print traceback.extract_stack()[-1][2]
		req = LoginGuildMemberSyn()
		req.user_seq = input("user_seq : ")
		req.guild_seq = input("guild_seq : ")
		req.nickname = 'spencer'
		req.profile_url = 'naver.com'
		req.level = 50
		req.server_id = 1
		self.client.send(req)
		
	def Syn_LogoutGuildMemberSyn(self) :
		print traceback.extract_stack()[-1][2]
		req = LogoutGuildMemberSyn()
		req.user_seq = input("user_seq : ")
		req.guild_seq = input("guild_seq : ")
		self.client.send(req)
		
	def Syn_CreateGuildSyn(self) :
		print traceback.extract_stack()[-1][2]
		req = CreateGuildSyn()
		req.user_seq = input("user_seq : ")
		req.gold = 10000
		req.level = 50
		req.guild_name = '1111'
		req.password = '1111'
		req.notice_text = 'Welcome to 1111'
		req.socialid = 9876678900123456
		req.nickname = 'spencer'
		req.profile_url = 'naver.com'
		req.exposure = 1
		req.server_id = 1
		self.client.send(req)
		
	def Ack_CreateGuildAck(self, recv_data) :
		print traceback.extract_stack()[-1][2]
		ack = CreateGuildAck()
		ack.Load(recv_data)
		print 'result(%d) guild(%d)' % (ack.result, ack.guild_seq)


	def Syn_JoinGuild(self) :
		print traceback.extract_stack()[-1][2]
		syn = JoinGuildSyn()
		syn.user_seq = input("user_seq : ")
		syn.guild_seq = input("guild_seq : ")
		syn.level = 15
		syn.password = "1234"
		syn.socialid = 88765432123456789
		syn.nickname = "Mega"
		syn.profile_url = "http://bbbb.jpg"
		syn.server_id = 1
		self.client.send(syn)

	def Ack_JoinGuild(self, data) :
		print traceback.extract_stack()[-1][2]
		ack = JoinGuildAck()
		ack.Load(data)
		print "user_seq(%d), result(%d)" % (ack.user_seq, ack.result)


	def Syn_LeaveGuild(self) :
		print traceback.extract_stack()[-1][2]
		syn = LeaveGuildSyn()
		syn.user_seq = input("user_seq : ")
		syn.guild_seq = input("guild_seq : ")
		self.client.send(syn)

	def Ack_LeaveGuild(self, data) :
		print traceback.extract_stack()[-1][2]
		ack = LeaveGuildAck()
		ack.Load(data)
		print "user_seq(%d), result(%d)" % (ack.user_seq, ack.result)

	def Syn_KickoutGuild(self) :
		print traceback.extract_stack()[-1][2]
		syn = KickoutGuildMemberSyn()
		syn.user_seq = input("user_seq : ")
		syn.guild_seq = input("guild_seq : ")
		syn.member_seq = input("member_seq : ")
		self.client.send(syn)

	def Ack_KickoutGuild(self, data) :
		print traceback.extract_stack()[-1][2]
		ack = KickoutGuildMemberAck()
		ack.Load(data)
		print "user_seq(%d), result(%d)" % (ack.user_seq, ack.result)

	def Syn_DisbandGuild(self) :
		print traceback.extract_stack()[-1][2]
		syn = DisbandGuildSyn()
		syn.user_seq = input("user_seq : ")
		syn.guild_seq = input("guild_seq : ")
		self.client.send(syn)

	def Ack_DisbandGuild(self, data) :
		print traceback.extract_stack()[-1][2]
		ack = DisbandGuildAck()
		ack.Load(data)
		print "user_seq(%d), result(%d)" % (ack.user_seq, ack.result)

	def Syn_ReloadResource(self) :
		print traceback.extract_stack()[-1][2]
		syn = ReloadResourceSyn()
		self.client.send(syn)



	def run(self) :
		while (True) :
			sys.stdout.write("\n")
			sys.stdout.write("0. LoginGuild\n")
			sys.stdout.write("1. LogoutGuild\n")
			sys.stdout.write("2. CreateGuild\n")
			sys.stdout.write("3. JoinGuild\n")
			sys.stdout.write("4. LeaveGuild\n")
			sys.stdout.write("5. KickoutGuild\n")
			sys.stdout.write("6. DisbandGuild\n")
			sys.stdout.write("99. Resource Reload\n")
			sys.stdout.flush()

			print "command : ",
			cmd = sys.stdin.readline()
			cmd = cmd.strip()

			if (cmd == "") : continue

			if ("0" == cmd) : self.Syn_LoginGuildMemberSyn()
			elif ("1" == cmd) : self.Syn_LogoutGuildMemberSyn()
			elif ("2" == cmd) : self.Syn_CreateGuildSyn()
			elif ("3" == cmd) : self.Syn_JoinGuild()
			elif ("4" == cmd) : self.Syn_LeaveGuild()
			elif ("5" == cmd) : self.Syn_KickoutGuild()
			elif ("6" == cmd) : self.Syn_DisbandGuild()
			elif ("99" == cmd) : self.Syn_ReloadResource()
